package com.wzy.util;

import com.opensymphony.xwork2.conversion.TypeConversionException;
import org.apache.struts2.util.StrutsTypeConverter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class DateConverter extends StrutsTypeConverter {
    @Override
    public Object convertFromString(Map context, String[] values, Class targetType) {
        try {
            String dateStr = values[0];
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy#MM#dd");

            return sdf.parse(dateStr);
        } catch (ParseException e) {
            throw new TypeConversionException("转换错误");
        }

    }

    @Override
    public String convertToString(Map context, Object object) {
        Date date = (Date) object;
        return new SimpleDateFormat("yyyy#MM#dd").format(date);
    }
}
